#include "friendmodel.hpp"
#include "db.h"
// 添加好友
void FriendModel::insert(int userid, int friendid)
{
    char sql[1024];
    sprintf(sql, "insert into Friend values(%d,'%d')", userid, friendid);
    MySQL mysql;
    if (mysql.connect())
    {
        if (mysql.update(sql))
        {
            return;
        }
    }
}

// 返回用户好友列表
vector<User> FriendModel::query(int userid)
{
    vector<User> vec;
    char sql[1024] = {0};
    sprintf(sql,
            "select id,name,state from User,Friend where id=friendid and Friend.userid = %d",
            userid);
    MySQL mysql;
    if (mysql.connect())
    {
        MYSQL_RES *res = mysql.query(sql);
        if (res != nullptr)
        {
            MYSQL_ROW row;
            while ((row = mysql_fetch_row(res)) != nullptr)
            {
                // 将userid的所有好友信息全部放入vector中返回
                User user;
                user.setId(atoi(row[0]));
                user.setName(row[1]);
                user.setState(row[2]);
                vec.push_back(user);
                
            }
            mysql_free_result(res);
            return vec;
        }
    }
    return vec;
}